package org.dreamwork.modbus;

public interface Predefined {
    interface Function {
        /** 都线圈 */
        int Read_Coils = 0x01;

        /** 读离散输入 */
        int Read_Discrete_Inputs = 0x02;

        /** 读保存寄存器 */
        int Read_Holding_Registers = 0x03;

        /** 读输入寄存器 */
        int Read_Input_Registers = 0x04;

        /** 写单个线圈 */
        int Write_Single_Coil = 0x05;

        /** 写单个寄存器 */
        int Write_Single_Register = 0x06;

        /** 读异常状态 (串行线路独有) */
        int Read_Exception_Status = 0x07;

        /** 诊断 */
        int Diagnostics = 0x08;

        /** 查询通用事件数量 (仅串行线路) */
        int Get_Comm_Event_Counter = 0x0b;

        /** 查询通用事件日志 (仅串行线路) */
        int Get_Comm_Event_Log = 0x0c;

        /** 写多个线圈 */
        int Write_Multiple_Coils = 0x0f;

        /** 写多个寄存器 */
        int Write_Multiple_Registers = 0x10;

        /** 上报服务器ID (仅串行线路) */
        int Report_Server_ID = 0x11;

        /** 读文件 */
        int Read_File_Record = 0x14;

        /** 写文件 */
        int Write_File_Record = 0x15;

        /** 屏蔽写寄存器 */
        int Mask_Write_Register = 0x16;

        int[] KNOWN_FUNCTIONS = {
            Read_Coils,             Read_Discrete_Inputs,   Read_Holding_Registers, Read_Input_Registers,
            Write_Single_Coil,      Write_Single_Register,  Read_Exception_Status,  Diagnostics,
            Get_Comm_Event_Counter, Get_Comm_Event_Log,     Write_Multiple_Coils, Write_Multiple_Registers,
            Report_Server_ID,       Read_File_Record,       Write_File_Record,      Mask_Write_Register
        };

        default boolean isKnown (int code) {
            for (int func : KNOWN_FUNCTIONS) {
                if (func == code) {
                    return true;
                }
            }
            return false;
        }
    }

    interface Exception {
        /** 非法功能码 */
        int ILLEGAL_FUNCTION = 0x01;

        /** 非法数据地址 */
        int ILLEGAL_DATA_ADDRESS = 0x02;

        /** 非法数据 */
        int ILLEGAL_DATA_VALUE = 0x03;

        /** 服务器内部错误 */
        int SERVER_DEVICE_FAILURE = 0x04;

        /** ack */
        int ACKNOWLEDGE = 0x05;

        /** 设备忙(无响应) */
        int SERVER_DEVICE_BUSY = 0x06;

        /** 内存奇偶校验错误 */
        int MEMORY_PARITY_ERROR = 0x08;

        /** 网关不可达 */
        int GATEWAY_PATH_UNAVAILABLE = 0x0a;

        /** 网关目标设备未响应 */
         int GATEWAY_TARGET_DEVICE_FAILED_TO_RESPOND = 0x0b;
    }
}
